home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / clx.lha / clx / excldep.c < prev    next >
C/C++ Source or Header  |  1988-09-12  |  3KB  |  138 lines

  1. /*
  2.  * Allegro CL dependent C helper routines for CLX
  3.  */
  4.  
  5. #include <sys/types.h>
  6. #include <sys/errno.h>
  7. #include <sys/time.h>
  8. #include <sys/ioctl.h>
  9. #include <stdio.h>
  10. #include <signal.h>
  11.  
  12. #define ERROR -1
  13. #define INTERRUPT -2
  14.  
  15. extern int errno;
  16.  
  17.  
  18. int c_check_bytes(fd, how_many)
  19.     int fd, how_many;
  20. {
  21.     int numavail;
  22.  
  23.     if (ioctl(fd, FIONREAD, (char *)&numavail) < 0) {
  24.     perror("c_check_bytes");
  25.     return (ERROR);
  26.     }
  27.  
  28.     if (numavail >= how_many)
  29.     return (1);
  30.     else
  31.     return (0);
  32. }
  33.  
  34.        
  35. /*
  36.  * Tries to read (end-start) characters into array at position start.
  37.  * This routine may only be called when enough data is available on the socket,
  38.  * otherwise we will block, which will prevent lisp from ever getting
  39.  * control again.  Return ERROR on eof or error.
  40.  */
  41.  
  42. int c_read_bytes(fd, array, start, end)
  43.     int fd, start, end;
  44.     unsigned char *array;
  45.  
  46. {
  47.     int numwanted;
  48.  
  49.     numwanted = end - start;
  50.     if (read(fd, (char *)&array[start], numwanted) < numwanted)
  51.     return (ERROR);
  52.     else
  53.     return (numwanted);
  54. }
  55.  
  56.  
  57. /*
  58.  * This is somewhat gross.  When the scheduler is not running we must provide
  59.  * a way for the user to interrupt the read from the X socket from lisp.  So
  60.  * we provide a separate reading function.
  61.  */
  62.  
  63. int c_read_bytes_interruptible(fd, array, start, end)
  64.     int fd, start, end;
  65.     unsigned char *array;
  66.  
  67. {
  68.     int numwanted, i, readfds;
  69.  
  70.     readfds = 1 << fd;
  71.     numwanted = end - start;
  72.  
  73.     i = select(32, &readfds, (int *)0, (int *)0, (struct timeval *)0);
  74.     if (i < 0)
  75.     /* error condition */
  76.     if (errno == EINTR)
  77.         return (INTERRUPT);
  78.     else
  79.         return (ERROR);
  80.  
  81.     if (read(fd, (char *)&array[start], numwanted) < numwanted)
  82.     return (ERROR);
  83.     else
  84.     return (numwanted);
  85. }
  86.  
  87.  
  88.  
  89. #define OBSIZE 4096    /* X output buffer size */
  90.  
  91. static unsigned char output_buffer[OBSIZE];
  92. static int obcount = 0;
  93.  
  94. /*
  95.  * The inverse of above, which is simpler because there's no timeout. 
  96.  * Don't need to block SIGIO's here, since the write either happens or it
  97.  * fails, it doesn't block.
  98.  */
  99. int c_write_bytes(fd, array, start, end)
  100.     int fd, start, end;
  101.     unsigned char *array;
  102. {
  103.     int numwanted, i;
  104.     void bcopy();
  105.  
  106.     numwanted = end - start;
  107.  
  108.     if (numwanted + obcount > OBSIZE)
  109.     /* too much stuff -- we gotta flush. */
  110.     if ((i = c_flush_bytes(fd)) < 0)
  111.         return (i);
  112.  
  113.     /* everything's cool, just bcopy */
  114.     bcopy((char *)&array[start], (char *)&output_buffer[obcount],
  115.       numwanted);
  116.     obcount += numwanted;
  117.  
  118.     return (numwanted);
  119. }
  120.  
  121. int c_flush_bytes(fd)
  122.     int fd;
  123. {
  124.     int i = 0, j = 0;
  125.  
  126.     while (obcount > 0) {
  127.     i = write(fd, (char *)(&output_buffer[j]), obcount);
  128.     if (i > 0) {
  129.         obcount -= i;
  130.         j += i;
  131.     }
  132.     else
  133.         return (ERROR);
  134.     }
  135.  
  136.     return (i);
  137. }
  138.